<?php
/**
 * 银联处理
 *
 * @category 
 * @package 
 * @author sunjing<sunjing@shopex.cn>
 * @version $Id: Z
 */
class erpapi_unionpay_request_unionpay extends erpapi_unionpay_request_abstract
{

    const _TO_NODE_ID = '1705101437';
    /**
     * 物流查询
     *
     * @return void
     * @author
     **/
    public function query($sdf)
    {
        $params = array(
            'to_node_id'    => self::_TO_NODE_ID,
            'tid'           => $sdf['tid'],
            'company_code'  => $sdf['company_code'],
            'company_name'  => $sdf['company_name'],
            'order_id'      => $sdf['logistic_code'],
            'device_type'   =>  'Android',
        );

        $title = sprintf('查询物流信息[%s]',$params['order_id']);

        $result= $this->__caller->call(STORE_LOGISTICS_TRACE_GET,$params,array(),$title,5,$params['logistic_code']);
        if($result['rsp'] == 'succ'){
            $data = json_decode($result['data'],true);
            $data = json_decode($data['data'],true);
            return array('rsp'=>'succ','data'=>$data['wlInfoList']);
        }
    }


    /**
     * 绑定
     *
     * @return void
     * @author 
     **/
    public function bind()
    {
        $params = array(
            'app'           => 'app.applyNodeBind',
            'node_id'       => base_shopnode::node_id('ome'),
            'from_certi_id' => base_certificate::certi_id(),
            'callback'      => '',
            'sess_callback' => '',
            //'api_url'       => 'http://210.22.91.77:24511/front_xgb/FrontService',
            'api_url'         =>'http://180.166.112.20:4111/front_xgb/FrontService',
            'node_type'     => 'ums',
            'to_node'       => self::_TO_NODE_ID,
            'shop_name'     => '商派',
            "api_secret"    => "c582cd9c33ca4fedb887d15fc46689cf",
        );

        $params['certi_ac']=$this->_gen_bind_sign($params);

        $title = '银联绑定';

        $callback = array();
        
        $result = $this->__caller->call(SHOP_LOGISTICS_BIND, $params, $callback, $title,4);

        $data = array('rsp'=>'succ');
        if ($result['res'] == 'succ' || $result['msg']['errorDescription'] == '绑定关系已存在,不需要重复绑定' ) {
            base_kvstore::instance('ome/bind/unionpay')->store('ome_bind_unionpay', true);

        } else {
            $data = array('rsp'=>'fail');
        }
        return $data;
    }

    private function _gen_bind_sign($params)
    {
        $token = base_certificate::token();

        ksort($params);
        $str = '';
        foreach ($params as $key =>$value) {
            $str .= $value;
        }

        $sign = md5($str.$token);

        return $sign;
    }

    /**
     * 绑定
     *
     * @return void
     * @author
     **/
    public function unbind()
    {
        $params = array(
            'app'           => 'app.changeBindRelStatus',
            'from_node'       => base_shopnode::node_id('ome'),
            'from_certi_id' => base_certificate::certi_id(),
            'node_type'     => 'ums',
            'to_node'       => self::_TO_NODE_ID,
            'status'        =>  'del',
            'reason'        =>  '重新解绑啦',
        );

        $params['certi_ac']=$this->_gen_bind_sign($params);

        $title = '银联解除绑定';

        $callback = array();

        $result = $this->__caller->call(SHOP_LOGISTICS_BIND, $params, $callback, $title,4);

        if ($result['res'] == 'succ' ) {
            base_kvstore::instance('ome/bind/unionpay')->store('ome_bind_unionpay', false);
            return true;
        } else {
            return false;
        }
    }
}